<template>
  <div>
    <van-list
    v-model="loading"
    :finished="finished"
    finished-text="没有更多了"
    :error.sync="error"
    error-text="请求失败，点击重新加载"
    @load="onLoad"
    >
    <comment-item @click-reply="$emit('click-reply', $event)" v-for="item in list" :key="item.com_id + ''" :comment="item" ></comment-item>
    </van-list>
</div>
</template>

<script>
import { getCommentList } from '@/api/comment.js'
import commentItem from '../comment-item'
export default {
  name: 'comments-list',
  data () {
    return {
      loading: false,
      finished: false,
      offset: null,
      limit: 10,
      error: false
    }
  },
  components: {
    commentItem
  },
  props: {
    source: {
      type: [Number, String, Object],
      required: true
    },
    list: {
      type: Array,
      required: true
    },
    type: {
      type: String,
      default: 'a'
    }
  },
  created () {

  },

  methods: {
    async onLoad () {
      try {
        // 1异步更新数据
        const { data: res } = await getCommentList({
          type: this.type,
          source: this.source + '',
          offset: this.offset,
          limit: this.limit
        })
        // console.log(res)
        // 2加载状态结束
        this.list.push(...res.data.results)
        // 3数据本次加载完成
        this.loading = false
        this.offset = res.data.last_id
        this.$emit('update-count', res.data.total_count)
        // 4数据全部加载完成if
        if (this.list.length >= res.data.total_count) {
          this.finished = true
        }
      } catch (err) {
        console.dir(err)
        this.$toast.fail('获取数据失败')
        this.error = true
        this.loading = false
      }
    }
  }
}
</script>

<style scoped lang='less'>

</style>
